

<!DOCTYPE html>
<html lang="zh-TW" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/assets/3.jpg">
  <link rel="icon" href="/assets/3.jpg">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Yong">
  <meta name="keywords" content="">
  
    <meta name="description" content="文中的演示代码基于CPython&#x2F;Python 3.9">
<meta property="og:type" content="article">
<meta property="og:title" content="Python魔法函数之__new__">
<meta property="og:url" content="http://ydhuyong.github.io/2024/03/11/%E9%AD%94%E6%B3%95%E5%87%BD%E6%95%B0/index.html">
<meta property="og:site_name" content="Yong&#39;s 书桌">
<meta property="og:description" content="文中的演示代码基于CPython&#x2F;Python 3.9">
<meta property="og:locale" content="zh_TW">
<meta property="og:image" content="http://ydhuyong.github.io/assets/003.svg">
<meta property="article:published_time" content="2024-03-11T01:09:02.734Z">
<meta property="article:modified_time" content="2024-03-13T04:47:57.753Z">
<meta property="article:author" content="Yong">
<meta property="article:tag" content="Python">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="http://ydhuyong.github.io/assets/003.svg">
  
  
  
  <title>Python魔法函数之__new__ - Yong&#39;s 书桌</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/KaTeX/0.16.2/katex.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"ydhuyong.github.io","root":"/","version":"1.9.7","typing":{"enable":false,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"python"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":{"measurement_id":null},"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml","include_content_in_search":false};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 7.0.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Yong&#39;s 书桌</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/" target="_self">
                <i class="iconfont icon-home-fill"></i>
                <span>首頁</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/" target="_self">
                <i class="iconfont icon-archive-fill"></i>
                <span>歸檔</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/" target="_self">
                <i class="iconfont icon-user-fill"></i>
                <span>關於</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle">Python魔法函数之__new__</span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2024-03-11 09:09" pubdate>
          2024年3月11日 上午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          1k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          14 分鐘
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <h1 id="seo-header">Python魔法函数之__new__</h1>
            
            
              <div class="markdown-body">
                
                <p>文中的演示代码基于CPython/Python 3.9</p>
<span id="more"></span>
<p>阅读Python 3 <code>collections</code>的<code>namedtuple</code>源码时，被<code>__new__</code>弄得很糊涂，翻看了官方教程和源码，藉此梳理</p>
<h2 id="引言">引言</h2>
<p>如<em>代码1</em>所示，定义一个<code>User</code>类，并实例化两个对象</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># 代码.1</span><br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">User</span>:<br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">__new__</span>(<span class="hljs-params"> cls, *args, **kwargvs</span>):<br>        <span class="hljs-built_in">print</span>( <span class="hljs-string">f&quot;In <span class="hljs-subst">&#123;cls&#125;</span> new function&quot;</span>)<br>        <span class="hljs-keyword">return</span> <span class="hljs-built_in">super</span>().__new__( cls)<br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, name = <span class="hljs-string">&quot;Juboge&quot;</span></span>):<br>        self.name = name<br>        <span class="hljs-built_in">print</span>( <span class="hljs-string">f&quot;In <span class="hljs-subst">&#123;self&#125;</span> init function, with name = <span class="hljs-subst">&#123;name&#125;</span>&quot;</span>)<br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&quot;__main__&quot;</span>:<br>    student = User( <span class="hljs-string">&quot;One&quot;</span>)<br>    <span class="hljs-built_in">print</span>()<br>    student_two = User( <span class="hljs-string">&quot;Two&quot;</span>)<br></code></pre></td></tr></table></figure>
<p>执行结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs shell">In &lt;class &#x27;__main__.User&#x27;&gt; new function<br>In &lt;__main__.User object at 0x000001CD83CACBE0&gt; init function, with name = One<br><br>In &lt;class &#x27;__main__.User&#x27;&gt; new function<br>In &lt;__main__.User object at 0x000001CD83CACD30&gt; init function, with name = Two<br></code></pre></td></tr></table></figure>
<p>Python的解释器在执行时，<code>new</code>和<code>init</code>的第一个参数有区别</p>
<ul>
<li>遇到<code>new</code>魔法函数时，第一个参数传入的是类本身<code>class</code></li>
<li>遇到<code>init</code>魔法函数时，第一个参数传入的是类的实例化对象<code>instance</code></li>
</ul>
<blockquote>
<p><code>cls</code>和<code>self</code>的命名是一种约定习惯，并非强制规范，这么命名避免混淆</p>
</blockquote>
<p><code>student = User( &quot;One&quot;)</code>语句依次调用了这两个魔法函数，<code>new</code>调用在<code>init</code>之前，<code>new</code>方法用于实例化对象，<code>init</code>方法用于初始化对象。<strong>new方法的作用，是控制类的实例化过程</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs python">student = User( <span class="hljs-string">&quot;One&quot;</span>)<br><span class="hljs-comment"># 等价于</span><br>student = __new__( User, ( <span class="hljs-string">&quot;One&quot;</span>, ) , &#123;&#125; )<br>__init__( student, ( <span class="hljs-string">&quot;One&quot;</span>, ), &#123;&#125; )<br></code></pre></td></tr></table></figure>
<h2 id="metaclass关键字">metaclass关键字</h2>
<p>如<em>代码2</em> 所示，引入<code>metaclass</code>关键字</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># 代码.2</span><br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">UserMetaClass</span>( <span class="hljs-title class_ inherited__">type</span>):<br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">__new__</span>(<span class="hljs-params"> cls, *args, **kwargs</span>):<br>        <span class="hljs-built_in">print</span>( <span class="hljs-string">f&quot;In <span class="hljs-subst">&#123;cls&#125;</span> new function&quot;</span>)<br>        <span class="hljs-keyword">return</span> <span class="hljs-built_in">super</span>().__new__(  cls, *args, **kwargs )<br><br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">NewUser</span>( metaclass = UserMetaClass):<br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">__new__</span>(<span class="hljs-params"> cls, *args, **kwargvs</span>):<br>        <span class="hljs-built_in">print</span>( <span class="hljs-string">f&quot;In <span class="hljs-subst">&#123;cls&#125;</span> new function&quot;</span>)<br>        <span class="hljs-keyword">return</span> <span class="hljs-built_in">super</span>().__new__( cls)<br>        <br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, name = <span class="hljs-string">&quot;Juboge&quot;</span></span>):<br>        <span class="hljs-built_in">print</span>( <span class="hljs-string">f&quot;In <span class="hljs-subst">&#123;self&#125;</span> new function&quot;</span>)<br>        self.name = name<br><br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&#x27;__main__&#x27;</span>:<br>    <span class="hljs-comment"># nstudent = NewUser( &quot;One&quot;)</span><br>    <span class="hljs-keyword">pass</span><br></code></pre></td></tr></table></figure>
<p>执行结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">In &lt;class &#x27;__main__.UserMetaClass&#x27;&gt; new function<br></code></pre></td></tr></table></figure>
<p>在解释 <em>代码2</em> 结果之前，必须先强调一个重要的概念——<strong>一切皆对象</strong>，这个一切不限于：</p>
<ul>
<li>函数 function</li>
<li>模板 module</li>
<li>变量 variable</li>
<li>实例 instance</li>
<li>类 class</li>
</ul>
<p>没错，<em><strong>类也是对象</strong></em></p>
<h3 id="两个类">两个类</h3>
<p>python中有两个重要的类：</p>
<ul>
<li><code>object</code>：python的始祖类，是继承树的根节点，都由<code>object</code>类派生或者迭代派生而来</li>
<li><code>type</code>: python的模板类，python中的任何对象，都由<code>type</code>类实例化或者迭代实例化而来</li>
</ul>
<p><code>object</code>类和<code>type</code>类他俩啥关系？他们同样适用这两个规则，<code>object</code>是<code>type</code>的实例化对象，<code>object</code>同时又是<code>type</code>的基类。</p>
<p>因此<code>issubclass( type, object)</code>和<code>isinstance( object, type)</code>这两条语句的返回值都是<code>True</code></p>
<p>举一个具体一点的例子：</p>
<p>字符串<code>&quot;abc&quot;</code>它是<code>str</code>类的实例化对象，而<code>str</code>类本身，是<code>type</code>类的实例化对象。因此<code>&quot;abc&quot;.__class__.__class__</code>的值为<code>type</code>，<em>代码1</em>中的<code>User</code>类，它当然也是<code>type</code>类的实例化对象，<code>student.__class__.__class__</code>的值也为<code>type</code></p>
<p>如图所示，是字符串<code>“abc”</code>，字符串类<code>string</code>、<code>object</code>类和<code>type</code>类之间的关系：</p>
<p><img src="/assets/003.svg" srcset="/img/loading.gif" lazyload alt="插图1"></p>
<h3 id="metaclass">metaclass</h3>
<p><em>代码1</em> 中的类定义语句，缺省条件下，默认继承<code>object</code>，元类是<code>type</code></p>
<figure class="highlight angelscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs angelscript"><span class="hljs-keyword">class</span> <span class="hljs-symbol">User:</span><br><span class="hljs-symbol">    <span class="hljs-symbol">pass</span></span><br># 由于类的声明没有加其它关键字，等价于：<br><span class="hljs-symbol">class</span> <span class="hljs-symbol">User</span>( <span class="hljs-symbol">object, <span class="hljs-symbol">metaclass</span></span> = <span class="hljs-symbol">type</span>):<br>    <span class="hljs-symbol">pass</span><br></code></pre></td></tr></table></figure>
<p>回到代码2，<code>Newuser</code>引入了<code>metaclass = UserMetaClass</code>语句，将自己的元类设成了<code>UserMetaClass</code>，也就是说<code>NewUser</code>被视为<code>UserMetaClass</code>的实例化对象。</p>
<blockquote>
<p>元类需要继承<code>type</code></p>
</blockquote>
<p>在Python解释器视角下，<code>NewUser</code>的定义过程，即是<code>UserMetaClass</code>类实例化的过程。上一章我们说过，<code>new</code>方法用于控制类的实例化过程，因此定义<code>NewUser</code>时，会调用<code>UserMetaClass</code>的<code>new</code>方法</p>
<figure class="highlight haskell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs haskell"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-type">NewUser</span>( <span class="hljs-title">metaclass</span> = <span class="hljs-type">UserMetaClass</span>):</span><br><span class="hljs-class"># 等价于</span><br><span class="hljs-class"><span class="hljs-type">NewUser</span> = <span class="hljs-type">UserMetaClass</span>.__new__( <span class="hljs-type">UserMetaCLass</span>, *<span class="hljs-title">args</span>, **<span class="hljs-title">kwargs</span> )</span><br></code></pre></td></tr></table></figure>
<p>这也是<em>代码2</em> 会打印信息的原因。<em>代码2</em> 执行过程，<code>NewUser</code>类自身的<code>new</code>方法没有被调用过，因为<code>NewUser</code>没有实例化对象。</p>
<h2 id="尾记">尾记</h2>
<p>本篇博客着重强调了两个概念：</p>
<ol>
<li><strong>new魔法函数用于控制类的实例化过程</strong></li>
<li><strong>类也是对象</strong></li>
</ol>
<p><em>代码1</em> 中的的<code>new</code>方法执行了两次，因为<code>User</code>类实例化了两次；而<em>代码2</em> 中的<code>new</code>方法只执行了一次，因为<code>UserMetaClass</code>只实例化了一次（定义<code>NewUser</code>类）。很多博客会介绍<code>new</code>方法，如果不强调<strong>类也是对象</strong>，我们很容易将类自身的<code>new</code>方法和元类的<code>new</code>方法混淆。</p>
<p>笔者将在下一篇博客中探讨：</p>
<ul>
<li>[ ] <code>type</code>类</li>
<li>[ ] <code>metaclass</code>的传参过程</li>
</ul>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Python/" class="print-no-link">#Python</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Python魔法函数之__new__</div>
      <div>http://ydhuyong.github.io/2024/03/11/魔法函数/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Yong</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>發布於</div>
          <div>2024年3月11日</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>更新於</div>
          <div>2024年3月13日</div>
        </div>
      
      
        <div class="license-meta-item">
          <div>許可協議</div>
          <div>
            
              
              
                <a class="print-no-link" target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2024/03/11/0689/" title="0698.深度优先搜索">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">0698.深度优先搜索</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目錄</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  


  
  








    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜尋</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">關鍵字</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.4/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>





  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.20.1/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允許 JavaScript 運行的環境下瀏覽效果更佳</div>
  </noscript>
</body>
</html>
